'''
Log tool for the spider

Created on 2013-5-25

@author: Soul
'''
import logging
import threading
from Configurator import Configurator

from WebkitLoginHelper import GtkLoginForm

class Logger:
    
    instance = None
    mutex = threading.Lock()
    
    def init(self):
        LOG_FMT = '%(asctime)s %(filename)s[line:%(lineno)d] %(threadName)-12s: %(levelname)s %(message)s'
        DATE_FMT = '%d %b %Y %H:%M:%S'
        log_file = Configurator.get_val('log_file')
        
        import os
        if not os.path.exists(log_file):
            # os.makedirs(log_file)
            f = open(log_file, 'w')
            f.close()
        
        logging.basicConfig(filename=log_file, format=LOG_FMT, datefmt=DATE_FMT, level=logging.INFO)
        
        # install console handler
        console = logging.StreamHandler()
        console.setLevel(logging.INFO)
        console.setFormatter(logging.Formatter('%(threadName)-12s: %(levelname)-8s %(message)s'))
        logging.getLogger('').addHandler(console)
    
    def error(self, msg):
        logging.error(msg)
    
    def warning(self, msg):
        logging.warn(msg)
    
    def info(self, msg):
        logging.info(msg)
    
    def debug(self, msg):
        logging.debug(msg)
    
    @staticmethod
    def get_instance():
        if(Logger.instance == None):
            Logger.mutex.acquire()
            if(Logger.instance == None):
                Logger.instance = Logger()
            Logger.mutex.release()
        
        return Logger.instance

logger = Logger.get_instance()
